import { createRouter, createWebHashHistory } from 'vue-router'

import Routes from '@/views/routes'

const router = createRouter({
  history: createWebHashHistory('/'),
  routes: Routes,
})

// 全局前置守卫：无 token 时跳转到登录页
router.beforeEach((to, _from, next) => {
  const token = localStorage.getItem('token')
  if (!token && to.name !== 'login') {
    next({ name: 'login' })
    return
  }
  next()
})

export default router
